﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Net;
using System.Net.Mail;

public partial class Admin_NhaXuatBan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HienThiDanhSach();

    }

    void HienThiDanhSach()
    {
        // Get All NhaXB
        DataTable Manufacturers = DataConnection.GetDatatableByStoreProcedureNameNoParam("sp_getNhaXuatBan");

        GridView1.DataSource = Manufacturers;
        GridView1.DataBind();

        MultiView1.ActiveViewIndex = 0;
        btninsert.Enabled = false;
    }


    void HienThiKetQuaSua(String manxb)
    {
        OracleParameter[] thamso = new OracleParameter[]
        {
            new OracleParameter("p_manhaxb",manxb)
        };
        DataTable dtnxb = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_getNXBByMaNXB", thamso);

        lbnxb.Text = dtnxb.Rows[0]["MaNhaXB"].ToString();
        txttennxb.Text = dtnxb.Rows[0]["TenNhaXB"].ToString();
        txtdiachi.Text = dtnxb.Rows[0][2].ToString();
        txtsodt.Text = dtnxb.Rows[0][3].ToString();
        txtemail.Text = dtnxb.Rows[0][4].ToString();

        Image2.ImageUrl = "~/image/nxb/" + dtnxb.Rows[0][5].ToString();



        MultiView1.ActiveViewIndex = 1;
    }


    protected void btntaomoi_Click(object sender, EventArgs e)
    {
        btnupdate.Visible = false;
        btninsert.Visible = true;
        MultiView1.ActiveViewIndex = 1;
        btninsert.Enabled = true;
        lbnxb.Text = "Mã tự động tăng";
        txttennxb.Text = "";

    }


    protected void btntmkiem_Click(object sender, EventArgs e)
    {
       
        string tennxb = txttim.Text;
        OracleParameter[] thamso = new OracleParameter[]{
            new OracleParameter("p_tennhaxb",tennxb)
        };

        // search NhaXB by TenNhaXB
        DataTable dt = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_SearchNhaXuatBanByTenNhaXB", thamso);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        txttim.Text = "";
    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.HienThiDanhSach();

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnupdate.Visible = true;
        btninsert.Visible = false;

        string manxb = GridView1.SelectedRow.Cells[2].Text;
        this.HienThiKetQuaSua(manxb);
    }
    protected void btnback_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }


    protected void btnupdate_Click(object sender, EventArgs e)
    {
        string manxb = lbnxb.Text;
        string tennxb = txttennxb.Text;       
        OracleParameter[] thamso = new OracleParameter[] {
            new OracleParameter("p_manhaxb", manxb),
        };

        // get NhaXB by MaNhaXB
        DataTable dt = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_getNXBByMaNXB", thamso);
        string hinh;
        if (FileUpload2.HasFile)
        {
            string strfile = "~/image/nxb/" + FileUpload2.FileName;
            string s = Server.MapPath(strfile);
            FileUpload2.SaveAs(s);
            hinh = FileUpload2.FileName;
        }
        else
            hinh = dt.Rows[0][5].ToString(); ;// dtsanpham.Rows[0]["Hinh"].ToString();



       
        OracleParameter[] thamso1 = new OracleParameter[]
        {
            
            new OracleParameter("p_tennxb",tennxb),
            new OracleParameter("p_diachi",txtdiachi.Text),
            new OracleParameter("p_sdt",txtsodt.Text),
            new OracleParameter("p_email",txtemail.Text),
            new OracleParameter("p_logo",hinh),
            new OracleParameter("p_manhaxb",manxb),

        };

        // update information of NhaXB by MaNXB
        DataConnection.GetDatatableByStoreProcedureName("update_NhaXBByMaNXB", thamso1);
        //this.HienThiDanhSach();
    }


    protected void btninsert_Click(object sender, EventArgs e)
    {
        //tao ma nha xuat ban tu dong tang = lay max manhaxb+1 
        int manhaxb= DataConnection.TruyVanMax("SELECT MAX(MANHAXB) FROM NHAXB");


        string tennxb = txttennxb.Text;
        string hinh = FileUpload2.FileName;

        
        OracleParameter[] thamso = new OracleParameter[]
        {
            new OracleParameter("p_manhaxb",manhaxb),
            new OracleParameter("p_tennxb",tennxb),    
            new OracleParameter("p_diachi",txtdiachi.Text),
            new OracleParameter("p_sdt",txtsodt.Text),
            new OracleParameter("p_email",txtemail.Text),
            new OracleParameter("p_logo",hinh)  
        };
        DataConnection.GetDatatableByStoreProcedureName("insertNhaXB", thamso);
        this.HienThiDanhSach();
    }


    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        string manxb = GridView1.Rows[e.RowIndex].Cells[2].Text;

        try
        {
            // use storeprocedure XOANHAXB

            OracleConnection con = DataConnection.TaoKetNoi();
            OracleCommand cmd = new OracleCommand("XOANHAXB", con);
            cmd.CommandType = CommandType.StoredProcedure;
            OracleParameter[] thamso = new OracleParameter[]
            {
                new OracleParameter("P_MANHAXB",manxb),
            };
            for (int i = 0; i < thamso.Length; i++)
            {
                cmd.Parameters.Add(thamso[i]);
            }
            cmd.ExecuteNonQuery();
        }
        catch
        {
            System.Console.WriteLine("khong xoa duoc do rang buoc khoa");
        }
        HienThiDanhSach();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
}